﻿using DualReferralServer.Service;
using DualReferralServer.Utils;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Web.Http;

namespace DualReferralServer.Controllers
{
    public class DualReferralController : ApiController
    {
        DualReferralService referralService = new DualReferralService();

        [HttpPost]
        [Route("DualReferral")]
        public HttpResponseMessage GetMethodType()
        {
            string method = "";
            LogHelper.Info(WebUtils.IPAddress + "访问接口 \n");
            try
            {
                foreach (string str in Request.Headers.GetValues("method"))
                {
                    method += str;
                }
            }
            catch (InvalidOperationException e)
            {
                Trace.WriteLine("请求头中并未包含指定的参数");
                LogHelper.Error(e.Message);
            }
            string temp;
            switch (method)
            {
                case "department-standard":
                    temp = referralService.GetDepartment(Request.Content.ReadAsStringAsync().Result);
                    LogHelper.Info("一级科室查询返回值: " + temp + "\n");
                    return new HttpResponseMessage { Content = new StringContent(temp, Encoding.GetEncoding("UTF-8"), "application/xml") };
                case "department-hospital":
                    temp = referralService.GetDepartmentlevel2(Request.Content.ReadAsStringAsync().Result);
                    LogHelper.Info("二级科室查询返回值: " + temp + "\n");
                    return new HttpResponseMessage { Content = new StringContent(temp, Encoding.GetEncoding("UTF-8"), "application/xml") };
                case "doctor":
                    temp = referralService.GetDoctors(Request.Content.ReadAsStringAsync().Result);
                    LogHelper.Info("医师查询返回值: " + temp + "\n");
                    return new HttpResponseMessage { Content = new StringContent(temp, Encoding.GetEncoding("UTF-8"), "application/xml") };
                case "bed-statistics":
                    temp = referralService.GetBedNums(Request.Content.ReadAsStringAsync().Result);
                    LogHelper.Info("床位统计查询返回值: " + temp + "\n");
                    return new HttpResponseMessage { Content = new StringContent(temp, Encoding.GetEncoding("UTF-8"), "application/xml") };
                case "bed-realtime":
                    temp = referralService.GetBedRealTime(Request.Content.ReadAsStringAsync().Result);
                    LogHelper.Info("床位实时查询返回值: " + temp + "\n");
                    return new HttpResponseMessage { Content = new StringContent(temp, Encoding.GetEncoding("UTF-8"), "application/xml") };
            }
            LogHelper.Info("没有相应的方法! \n");
            return new HttpResponseMessage { Content = new StringContent("没有相应的方法", Encoding.GetEncoding("UTF-8"), "application/text") };
        }
    }
}
